package com.example.recordingpen_datacollection.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.recordingpen_datacollection.Utils.DateUtils.TimeJudgeUtils;
import com.example.recordingpen_datacollection.Utils.String.StringUtils;
import com.example.recordingpen_datacollection.entity.Businessdata;
import com.example.recordingpen_datacollection.entity.LimitRatio;
import com.example.recordingpen_datacollection.entity.UserMessage;
import com.example.recordingpen_datacollection.entity.Vo.LimitDosageVo;
import com.example.recordingpen_datacollection.entity.Vo.VoucherVo;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.BusinessdataService;
import com.example.recordingpen_datacollection.service.LimitRatioService;
import com.example.recordingpen_datacollection.service.UserLoginService;
import com.example.recordingpen_datacollection.service.UserMessageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.util.Date;

import static com.example.recordingpen_datacollection.Utils.DateUtils.TimeJudgeUtils.FORMAT_1;

@RestController
@RequestMapping("/voucherCenter")
public class VoucherCenterController {
    @Autowired
    private RedisService redisService;
    //充值字数额度
    @Autowired
    private BusinessdataService businessdataService;
    //
    @Autowired
    private UserMessageService userMessageService;
    @Autowired
    private LimitRatioService limitRatioService;
    @Autowired
    private UserLoginService userLoginService;

    @ApiOperation(value = " 字数充值接口")
    @PostMapping("/txtDosage")
    public RLIST txtDosage(@RequestBody VoucherVo voucherVo, HttpServletRequest request) {
        String token = request.getHeader("token");

        if (!(token.length() == 51 || token.length() == 56)) {
            return RLIST.error().message("无权操作");
        }
        Integer value = null;
        if (token.length() == 51) {
            value = (Integer) redisService.getValue(token);
            boolean flat = userMessageService.verifyLimit(value);
            if(!flat){
                return RLIST.error().message("分配额度不足");
            }
            voucherVo.setAddDosage(100000L);
        }

        QueryWrapper wrapper = new QueryWrapper();
        Businessdata businessdata = new Businessdata();
        wrapper.eq("login_id", voucherVo.getLoginId());

        Businessdata one = businessdataService.getOne(wrapper);
        if (one == null) {
            return RLIST.error().message("账号不存在");
        }


        businessdata.setLoginId(voucherVo.getLoginId());
        businessdata.setDosage(voucherVo.getAddDosage() + one.getDosage());

        boolean update = businessdataService.updateDosage(businessdata, wrapper, value);
        if (update) {

            return RLIST.ok();
        } else {
            return RLIST.error();
        }

    }
    @ApiOperation(value = " vip充值接口ip 按月")
    @PostMapping("/vipDosage")
    public RLIST vipDosage(@RequestBody VoucherVo voucherVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if( !(token.length() == 51 || token.length() == 56) ){
            return RLIST.error().message("无权操作");
        }
        Integer value = null;
        if (token.length() == 51) {
            value = (Integer) redisService.getValue(token);
            boolean flat = userMessageService.verifyLimit(value);
            if(!flat){
                return RLIST.error().message("分配额度不足");
            }
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("uid",value);
            LimitRatio byId = limitRatioService.getOne(wrapper);
            if(byId == null){
                voucherVo.setMonth(18);
            } else {
                voucherVo.setMonth(byId.getRatio());
            }


        }
        Businessdata businessdata = new Businessdata();
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("login_id",voucherVo.getLoginId());
        Businessdata one = businessdataService.getOne(wrapper1);
        if(one == null){
            return RLIST.error().message("账号不存在");
        }

        //获取配额



        businessdata.setLoginId(voucherVo.getLoginId());
        //开始时间  结束时间
        String getEndTime = TimeJudgeUtils.strData(one.getEndTime());
        boolean judgeTime = TimeJudgeUtils.judgeTime(TimeJudgeUtils.getNowDate(), getEndTime);
        if(!judgeTime){
            String addDayTime = TimeJudgeUtils.addMonthTime(getEndTime, voucherVo.getMonth());
            Date addNowDayTime = TimeJudgeUtils.parseDate(addDayTime,"yyyy-MM-dd HH:mm:ss");
            businessdata.setEndTime(addNowDayTime);

        } else {
            Date nowDate = TimeJudgeUtils.parseDate(TimeJudgeUtils.getNowDate(), "yyyy-MM-dd HH:mm:ss");
            String s = TimeJudgeUtils.addNowDayTime(1) + " 23:59:59";
            String addDayTime = TimeJudgeUtils.addMonthTime(s, voucherVo.getMonth());
            Date addNowDayTime = TimeJudgeUtils.parseDate(addDayTime, "yyyy-MM-dd HH:mm:ss");
            businessdata.setStartTime(nowDate);
            businessdata.setEndTime(addNowDayTime);
        }


        {
            boolean judgeTime1 = TimeJudgeUtils.judgeTime(TimeJudgeUtils.strData(one.getMaxTime()), TimeJudgeUtils.strData(businessdata.getEndTime()));
            if(!judgeTime1){
                System.out.println("b1: "+judgeTime1);
                return RLIST.error().message("超出最大时间");
            }
        }


        boolean update = businessdataService.updateDosage(businessdata, wrapper1, value);
        if(update){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }

    }
    //充值VIP天数

    /**
     * 1 使用一个额度添加设备的最大时间
     */
    @ApiOperation(value = "配额时间分配接口 按月")
    @PostMapping("/limitDosage")
    public RLIST limitDosage(@RequestBody LimitDosageVo limitDosageVo, HttpServletRequest request){
        System.out.println(limitDosageVo.toString());
        String token = request.getHeader("token");
        if( !(token.length() == 51) ){
            return RLIST.error().message("无权操作");
        }
        Integer  value = (Integer) redisService.getValue(token);
        UserMessage byId = userMessageService.getById(value);

        //校验区
        if(limitDosageVo.getDevId() == null || limitDosageVo.getDevId()<=0){
            return RLIST.error().message("设备参数异常");
        }
        if(!limitDosageVo.isAtLeastOneMemberNotNull()){
            return RLIST.error().message("参数禁止全为空");
        }


         boolean verifyUser = userLoginService.verifyUser(value,limitDosageVo.getDevId());
        if(!verifyUser){
            return RLIST.error().message("无此成员");
        }
        //1 判断user 额度是否充足
        if(limitDosageVo.getLimit() !=null && byId.getLimitQuantity()<limitDosageVo.getLimit()){
            return RLIST.error().message("额度不足");
        }
        //判断时间格式和大小
        if (StringUtils.isNotEmpty(limitDosageVo.getEndTime())){
            limitDosageVo.setEndTime(limitDosageVo.getEndTime()+" 23:59:59");
            boolean validTimeFormat = TimeJudgeUtils.isValidTimeFormat(limitDosageVo.getEndTime(), FORMAT_1);
            if(!validTimeFormat){
                return RLIST.error().message("时间格式异常");
            }
        }
        boolean flat = businessdataService.operateLimitDosage(limitDosageVo,byId);



        return RLIST.ok();

    }

}
